package ua.pp.fland.multithreading.sorting;

import org.apache.commons.cli.ParseException;
import org.apache.log4j.Logger;

import java.io.IOException;

/**
 * @author Maxim Bondarenko
 * @version 1.0 Jun 29, 2010
 */

public class Sorting {
    private static final Logger logger = Logger.getLogger(Sorting.class);


    public static void main(String[] args) throws IOException, InterruptedException, ParseException {
        logger.info("App started...");

        logger.debug("Getting input params...");

        ArgsTokenizer argsTokenizer = new ArgsTokenizer(args);
        argsTokenizer.parseArgs();

        long startTime = System.currentTimeMillis();
        SortingController sortingController = new SortingController(argsTokenizer.getThreadsNum());

        DataInputController dataInputController;

        switch (argsTokenizer.getSetDataInput()) {
            case RANDOM_INPUT:
                dataInputController = new RandomDataInput(argsTokenizer.getDataNum());
                break;
            case FILE_INPUT:
                dataInputController = new FileDataInput(argsTokenizer.getFileName());
                break;
            default:
                logger.error("Got unexpected data input type: " + argsTokenizer.getSetDataInput());
                throw new IllegalArgumentException("Got unexpected data input type: " + argsTokenizer.getSetDataInput());
        }


        //sortingController.setUnsortedData(dataInputController.getUnsortedData());
        sortingController.sort(dataInputController.getUnsortedData());

        logger.info("Elapsed time: " + (System.currentTimeMillis() - startTime) + "ms");
    }
}
